<template>
  <div class="my-navigation">
    <van-swipe
      v-if="!isScroll && list.length !== 0"
      :vertical="isVertical"
      :autoplay="3000"
      :show-indicators="false"
    >
      <van-swipe-item  v-for="(data, key) of list" :key="key">
        <div class="navigation-content">
          <div class="icon" v-for="(item, index) of data" :key="index">
            <div class="icon-img-div">
              <img class="icon-img"
                :src="item.url || ''"
                :alt="item.name"
                :title="item.name"
              />
            </div>
            <p class="icon-desc">{{item.name || index}}</p>
          </div>
        </div>
      </van-swipe-item>
    </van-swipe>
    <ul class="navigation-ul-scroll-x" v-else>
      <li class="navigation-li-item" v-for="(item, index) of list" :key="index">
        <div class="icon">
          <div class="icon-img-div">
            <img class="icon-img"
              :src="item.url || ''"
              :alt="item.name"
              :title="item.name"
            />
          </div>
          <p class="icon-desc">{{item.name || index}}</p>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  props: {
    data: {
      type: Object,
      default: () => ({})
    }
  },
  computed: {
    elements () { // 待渲染的元素
      return this.data.elements || []
    },
    isScroll () { // 分页还是滚动(仅一行)
      return this.data.type === 'scroll'
    },
    x () { // 列数
      return Number(this.data.column) || 4
    },
    y () { // 行数
      return this.isScroll ? 1 : (Number(this.data.row) || 2)
    },
    list () {
      if (this.isScroll) return this.elements
      const pages = []
      this.elements.forEach((item, index) => {
        let page = Math.floor(index / (this.x * this.y))
        if (!pages[page]) {
          pages[page] = []
        }
        pages[page].push(item)
      })
      return pages
    },
    rowHeight () { // 元素行高度
      return Number(this.data.rowHeight) || 140
    },
    height () { // 容器高度
      return this.rowHeight * this.y
    },
    isVertical () { // 是否纵向
      return this.data.direction !== 'horizontal'
    },
    isShowImg () { // 是否显示图片
      return this.data.isShowImg
    },
    isShowText () { // 是否显示文本
      return this.data.isShowText
    },
    textHeight () { // 文本高度
      return this.isShowText ? (Number(this.data.textHeight) || 20) : 0
    },
    imgHeight () { // 图片高度
      return this.isShowImg ? (this.rowHeight - this.textHeight) : 0
    },
    scale () { // 图片缩放比例
      return Number(this.data.scale) || 0.75
    }
  },
  mounted () {
    if (this.$el) {
      const el = this.$el
      if (this.isScroll) {
        el.style.setProperty('--width', this.rowHeight + 'px')
      } else {
        el.style.setProperty('--width', `calc(100% / ${this.x})`)
      }
      el.style.setProperty('--height', `${this.height}px`)
      el.style.setProperty('--row-height', `${this.rowHeight}px`)
      el.style.setProperty('--text-height', this.textHeight + 'px')
      el.style.setProperty('--scale', this.scale)
    }
  }
}
</script>

<style lang="less" scoped>
.my-navigation {
  --width: 25%;
  --height: 80px; // 容器高度
  --row-height: 80px; // 行高
  --text-height: 20px; // 文本高
  --scale: .75;
  width: 100%;
  height: var(--height); // 容器高
  box-sizing: border-box;
  overflow: hidden;
  background-color: #fff;
  /**
   * 导航图标大小, 单行
   * 文字: 2-4个, 55*20/14px
   * 行高: 80
   * 宽度: 100%/列数
   * 图片为宽和高最小值缩放.75倍
   */
  .navigation-content {
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
    color: #333;
    &:after {
      clear: both;
    }
    .icon {
      position: relative;
      overflow: hidden;
      width: var(--width);
      height: var(--row-height);
      float: left;
      .icon-img-div {
        overflow: hidden;
        width: 100%;
        height: calc(100% - var(--text-height));
        line-height: 1;
        box-sizing: border-box;
        margin: 0 auto;
        // padding: 8px; // 使用了scale, 就不用padding了
        .icon-img {
          display: block;
          max-width: 100%;
          max-height: 100%;
          margin: 0 auto;
          transform: scale(var(--scale));
        }
      }
      .icon-desc {
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        display: block;
        font-size: 14px;
        height: var(--text-height);
        letter-spacing: -0.41px;
        text-align: center;
        margin: 0 auto;
        word-break: keep-all;
      }
    }
  }
  // 横向滚动
  .navigation-ul-scroll-x {
    position: relative;
    width: 100%;
    height: 100%;
    overflow-x: scroll;
    overflow-y: hidden;
    white-space: nowrap;
    &::-webkit-scrollbar {
      width: 0;
      height: 0;
    }
    .navigation-li-item {
      display: inline-block;
      height: 100%;
      margin-left: 5px;
      &:first-child {
        margin-left: 0;
      }
      .icon {
        position: relative;
        overflow: hidden;
        width: var(--width);
        height: var(--row-height);
        .icon-img-div {
          overflow: hidden;
          width: 100%;
          height: calc(100% - var(--text-height));
          line-height: 1;
          box-sizing: border-box;
          margin: 0 auto;
          // padding: 8px; // 使用了scale, 就不用padding了
          .icon-img {
            display: block;
            max-width: 100%;
            max-height: 100%;
            margin: 0 auto;
            transform: scale(var(--scale));
          }
        }
        .icon-desc {
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          display: block;
          font-size: 12px;
          // width: 55px;
          height: var(--text-height);
          // line-height: 1;
          letter-spacing: -0.41px;
          text-align: center;
          margin: 0 auto;
          word-break: keep-all;
        }
      }
    }
  }
}
</style>